package com.zjg.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.dao.BaseDao;
import com.common.entity.A_complain;
import com.common.entity.A_customer;
import com.common.entity.A_opinion;
import com.common.entity.PageBean;
import com.lyk.dao.A_customerDao;
import com.lyk.dao.impl.A_customerDaoImpl;
import com.zjg.dao.OpinionDao;

public class OpinionDaoImpl extends BaseDao<A_opinion> implements OpinionDao {
	private A_customerDao customerDao=new A_customerDaoImpl();
	private A_customer customer=null;
	
	@Override
	public List<A_opinion> findByCondition(Map<String, Object> map) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_opinion where 1=1 ");
		if(map!=null && map.size()>0){
			if(map.get("customer_id")!=null){
				sf.append("and customer_id=? ");
				paramList.add(map.get("customer_id"));
			}
			if(map.get("start_time")!=null){
				String str=(String) map.get("start_time");
				sf.append("and start_time > to_date('?','yyyy-mm-dd hh24:mi:ss')");
				if(str.contains("&"))
				{
					sf.append("and start_time < to_date('?','yyyy-mm-dd hh24:mi:ss')");
					String time[]=str.split("&");
					paramList.add(time[0]);
					paramList.add(time[1]);
				}
				else
				{
					paramList.add(str);
				}
			}
		}
		return this.findBySql(sf.toString(), paramList.toArray());
	}

	@Override
	public PageBean<A_opinion> findPageByCondition(int pageIndex, int pageSize,
			Map<String, Object> map) {
		PageBean<A_opinion> pageBean=new PageBean<A_opinion>();
		
		//1���ȳ�ʼ�����õ���ֵ
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);
		
		//2��ƴ��sql���
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer querySql=new StringBuffer();
		querySql.append(" select * from A_opinion where 1=1");
		if(map!=null && map.size()>0){
		}
		
		//3����ȡ�ܼ�¼��
		pageBean.setTotalCount(this.getCount(querySql.toString(), paramList.toArray()));
		
		//4����ѯ��ҳ���
		if(pageBean.getTotalCount()>0){
			StringBuffer queryPageSql=new StringBuffer();
			queryPageSql.append(" select * from (");
			queryPageSql.append("  select tmp.*,rownum rn from ( ");
			queryPageSql.append(querySql.toString());
			queryPageSql.append("  ) tmp ");
			queryPageSql.append(" ) where rn>? and rn<=? ");
			int startRow=(pageBean.getPageIndex()-1)*pageBean.getPageSize();
			int endRow=pageBean.getPageIndex()*pageBean.getPageSize();
			paramList.add(startRow);
			paramList.add(endRow);
			pageBean.setList(this.findBySql(queryPageSql.toString(), paramList.toArray()));
		}
		return pageBean;
	}

	@Override
	public A_opinion findById(Integer id) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from A_opinion where opinion_id=? ");
		paramList.add(id);
		List<A_opinion> list=this.findBySql(sf.toString(), paramList.toArray());
		return (list!=null && list.size()>0)?list.get(0):null;
	}

	@Override
	public Object saveOpinion(A_opinion entity) {
		String sql="insert into a_opinion(opinion_id,details,customer_id,start_date) values(seq_a_opinion.nextval,?,?,?)";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(entity.getDetails());
		paramList.add(entity.getCustomer_id());
		paramList.add(entity.getStart_date());
		return this.executeUpdate(sql, paramList.toArray(),new String[]{"opinion_id"});
	}

	@Override
	public int deleteOpinion(Integer id) {
		String sql="delete a_opinion where opinion_id=?";
		return (Integer) this.executeUpdate(sql, new Object[]{id},null);
	}

	@Override
	public A_opinion getEntity(ResultSet rs) {
		try {
			A_opinion entity=new A_opinion();
			entity.setOpinion_id(rs.getInt("Opinion_id"));
			entity.setDetails(rs.getString("Details"));
			entity.setCustomer_id(rs.getInt("Customer_id"));
			entity.setStart_date(rs.getTimestamp("Start_date"));
			customer=customerDao.findById(rs.getInt("Customer_id"));
			entity.setCustomer(customer);
			return entity;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int CountOpinion() {
		String sql = "a_opinion";
		return getCount(sql, null);
	}
}
